<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" />
      </template>
    </BasicForm>

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row: ListData) => row.corName"
      :dataSource="dataform"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    >
      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>

  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '../../components/Table';
  import { BasicForm, FormSchema, useForm } from '../../components/Form/index';
  import { getAllApply, handleApply } from '../../api/dataInfo/flightInfo';
  import { columns, ListData } from './columns';
  import { useDialog, useMessage } from 'naive-ui';
  import { ResultEnum, ipAddress } from '@/enums/httpEnum';


  const schemas: FormSchema[] = [
    {
      field: 'status',
      component: 'NSelect',
      label: '申请状态',
      componentProps: {
        placeholder: '请选择申请状态',
        options: [
          {
            label: '未处理',
            value: 0
          },
          {
            label: '已处理',
            value: 1
          }
        ],
        filterable: 'true',
      },
    }
  ];
  const message = useMessage();
  const actionRef = ref();
  const dataform = ref([]);
  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      const label = record.status === 1 ? '查看' : '处理';
      return h(TableAction, {
        style: 'button',
        actions: [
          {
            label: label,
            onClick: lookInfo.bind(null, record),
            // 根据业务控制是否显示 isShow 和 auth 是并且关系
            ifShow: () => {
              return true;
            },
          },
        ],
      });
    },
  });


  const [register, { getFieldsValue }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  const loadDataTable = async (res) => {
    console.log(res);
    console.log(getFieldsValue());
    const rese = await getAllApply({...getFieldsValue(), ...res});
    if(rese.code == ResultEnum.SUCCESS){
      var matchedName = ipAddress[rese.msg];
      message.success('正在访问'+matchedName+'的接口');
    }
    return rese.data;
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  const dialog = useDialog();
function lookInfo(record: Recordable) {
    if(record.status == 1 ){
      dialog.warning({
          title: '申请通过',
          content: '您无需处理',
        })
    }
    if(record.status == 0){
      dialog.warning({
          title: '通过还是不通过',
          content: '请选择',
          positiveText: '通过申请',
          negativeText: '拒绝申请',
          onPositiveClick: async () => {
            record.status = 1;
            console.log(record)

            const response = await handleApply(record);
            if(response.code == ResultEnum.SUCCESS){
              if(response.code == ResultEnum.SUCCESS){
                  var matchedName = ipAddress[response.msg];
                  message.success('正在访问'+matchedName+'的接口');
                }
              message.success('通过成功');
              reloadTable();
            }else{
              message.error("网络问题，请重试")
              reloadTable();
            } 
          },
          onNegativeClick: async() =>{
            record.status = 1;
            const response = await handleApply(record);
            if(response.code == ResultEnum.SUCCESS){
              if(response.code == ResultEnum.SUCCESS){
                  var matchedName = ipAddress[response.msg];
                  message.success('正在访问'+matchedName+'的接口');
                }
              message.success('拒绝成功');
              reloadTable();
            }else{
              message.error("网络问题，请重试")
              reloadTable();
            } 
          }
        })
    }
    
  }


  function handleReset(values: Recordable) {
    console.log(values);
  }

  function handleSubmit(){
    reloadTable();
  }
</script>

<style lang="less" scoped></style>
